import { Toast } from "vant";
import { nextTick, VueElement } from "vue";
import { userCartAdd, cartInitGet, userCartDel } from "../api/cart.js";

export default {
    namespaced: true,
    state() {
        return{
            cartList:[
                // {
                //     "id": 97458,
                //     "member_id": 143457,
                //     "product_discount_price": "168.00",
                //     "product_name": "Moon Cake 赏月",
                //     "product_packet_fee": "0.00",
                //     "product_price": "218.00",
                //     "product_quantity": 1,
                //     "product_spec_id": 554,
                //     "product_spec_name": "1盒：8枚",
                //     "product_spec_unit": 1,
                //     "isCheck":true,
                //     "pc_cover": "http://img.maixiaobu.cn/kissmilan/product/20220817/4580c445e1fe80db.jpg"
                //     },
            
                //     {
                //     "id": 97459,
                //     "member_id": 143457,
                //     "product_discount_price": "228.00",
                //     "product_name": "Blueberry Gelato 蓝莓冰淇淋蛋糕",
                //     "product_packet_fee": "0.00",
                //     "product_price": "228.00",
                //     "product_quantity": 2,
                //     "product_spec_id": 554,
                //     "product_spec_name": "1.5磅：直径约13cm",
                //     "product_spec_unit": 1,
                //     "isCheck":true,
                //     "pc_cover":"http://img.maixiaobu.cn/kissmilan/product/20220815/861f5252c1f31f21.jpg"
                //         }
            ]
        }
    },
    mutations: {
        cartCheckMut(state,index) {//单选
            state.cartList[index].isCheck=!state.cartList[index].isCheck
        },
        cartAllCheckMut(state,bool) {//全选控制单选
            let { cartList } = state
            cartList.forEach(el => {
                el.isCheck=!bool
            });
        },
        cartDelMut(state) {//删除购物车
            let { cartList } = state
            state.cartList=cartList.filter(item => {
                return !item.isCheck
            })
        },
        cartAddMut(state, newCart) {//加入购物车
            if (state.cartList instanceof Array) {
                // console.log("是数组");
                state.cartList.push(JSON.parse(newCart));
            } else if (state.cartList instanceof String) {
                // onsole.log("是字符串");
             }
            else { 
                // console.log("不是数组");
                state.cartList = [];
                state.cartList.push(JSON.parse(newCart));
            }
            // console.log("11111111", state.cartList);
            Toast.success('添加成功')
        },
        cartInitMut(state, cartList) {//初始化购物车
            console.log('初始化',cartList);
            state.cartList=cartList
        }
    },
    getters: {
        allInfo(state) {
            let { cartList } = state
            let checkNum = 0
            let allCheck = true
            let checkPrice = 0
            if (cartList.length) {
                cartList.forEach(item => {
                if (!item.isCheck) {
                    allCheck=false
                }
                if (item.isCheck) {
                    checkNum += item.product_quantity
                    checkPrice+=item.product_quantity*item.product_price*100
                }
            });
            }
            
            return {
                checkNum,
                allCheck,
                checkPrice
                }
        }
    },
    actions: {
        cartAddAct(context, cartObj) {//加入购物车 
            userCartAdd(cartObj).then(res => {
                let newCart = res.config.data
                // console.log('newCart111', newCart);
                context.commit('cartAddMut',newCart)
            })
        },
        cartInitAct(context, userInfo) {//初始化购物车
            // console.log(userInfo);
            let userid=userInfo._id
            cartInitGet(userid).then(res => {
                console.log("初始化购物车",res);
                context.commit('cartInitMut',res.data.data)
            })  
        },
        cartDelAct(context,{userid, id}) {//删除购物车
            userCartDel(userid, id).then((res) => {
                console.log("删除购物车", res);
                if (res.data.code==111) {
                    context.commit("cartDelMut");
              }
                  
           
                  
               
              });
            
        }
    }
}